package bot

import (
	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
	"lidongyooo/search-bot/pkg/config"
	"lidongyooo/search-bot/pkg/proxy"
	"log"
)

type MyBot struct {
	*tgbotapi.BotAPI
}

var (
	Bot *MyBot
	Me  tgbotapi.User
)


func New(token string) (*MyBot, error) {
	var gerr error
	if config.Viper.GetBool("PROXY.ENABLE") {
		bot, err := tgbotapi.NewBotAPIWithClient(token, tgbotapi.APIEndpoint, proxy.Client())
		gerr = err
		Bot = &MyBot{BotAPI: bot}
	} else {
		bot, err := tgbotapi.NewBotAPI(token)
		gerr = err
		Bot = &MyBot{BotAPI: bot}
	}

	if gerr != nil {
		panic(gerr)
	}
	Me, _ = Bot.GetMe()

	return Bot, gerr
}

func (bot *MyBot) SendMessage(chatId int64, text string) {
	msg := tgbotapi.NewMessage(chatId, text)
	_, err := bot.Send(msg)
	if err != nil {
		log.Println(err)
	}
}

func (bot *MyBot) SendReplyMessage(chatId int64, replyId int, text string) {
	msg := tgbotapi.MessageConfig{
		BaseChat: tgbotapi.BaseChat{
			ChatID: chatId,
			ReplyToMessageID: replyId,
		},
		Text:                  text,
		DisableWebPagePreview: false,
	}

	_, err := bot.Send(msg)
	if err != nil {
		log.Println(err)
	}
}